import pygame
import constants as c
import data
import bird

class Air(pygame.sprite.Sprite):

    def __init__(self, kind, a, b, m):
        pygame.sprite.Sprite.__init__(self)

        # pass in kind, velocity coeffs (pixels/s) and mass (kg)
        self.kind = kind
        self.a = a/1000.0
        self.b = b/1000.0
        self.m = m

        # load appropriate image
        if self.kind == c.BLOW:
            self.image, self.rect = data.load_image("blow.png", -1)
        else:
            self.image, self.rect = data.load_image("suck.png", -1)

        # set up the starting position of the air
        if self.kind == c.BLOW:
            self.starty = c.BLOW_START_Y
        else:
            self.starty = c.SUCK_START_Y
        self.rect.midtop = (pygame.mouse.get_pos()[0], self.starty)

    def update(self, time_diff):
        # find the distance between the air and its start point
        delta_y =  abs(self.rect.top - self.starty)

        # calculate its velocity as a function of delta_y
        if self.kind == c.BLOW:
            self.v = -1.0 * (self.a*delta_y + self.b)
        else:
            self.v = (self.a * delta_y + self.b)

        # update rect position
        self.rect.top = self.rect.y + self.v * time_diff

    def collide(self, other):
        'air has collided with another object'
        if isinstance(other, bird.Bird):
            self.kill()

